# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Renato Silva <br.renatosilva@gmail.com>

_realname=drmingw
pkgbase="mingw-w64-${_realname}"
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname})
pkgver=0.9.8
pkgrel=1
pkgdesc="Just-in-Time (JIT) debugger (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=(LGPL2.1)
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-discount"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-python"
             "git")
url='https://github.com/jrfonseca/drmingw'
source=("${_realname}-${pkgver}::git+https://github.com/jrfonseca/drmingw.git#tag=${pkgver}"
        0001-install-files.patch
        0002-cmake-fixes-for-clang.patch)
options=(!strip staticlibs)
sha256sums=('SKIP'
            '06c3c86d1c36e97fcfe1d1da737597ffe2e83d848c885714bd4d92de098ca74b'
            'a5339eeb9388b68e2de6fcd894000d86b03f5fabe56e2320ca5343ad08d70376')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  git submodule update --init --depth=1
  patch -p1 -i "${srcdir}/0001-install-files.patch"
  patch -p1 -i "${srcdir}/0002-cmake-fixes-for-clang.patch"
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir -p ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare _pthreads_opt="-DPOSIX_THREADS=OFF"
  if [[ ${MINGW_PACKAGE_PREFIX} != *-clang-* ]]; then
    _pthreads_opt="-DPOSIX_THREADS=ON"
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python.exe \
    ${_pthreads_opt} \
    ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake.exe --build ./
}

check() {
  cd "${srcdir}/build-${MSYSTEM}"
  ${MINGW_PREFIX}/bin/ctest.exe ./ || true
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake.exe --install .

  mv -v ${pkgdir}${MINGW_PREFIX}/bin/addr2line{,-drmingw}.exe

  # License files
  cd "${pkgdir}${MINGW_PREFIX}/share"
  mkdir -p licenses/${_realname}
  mv -v doc/${_realname}/LICENSE* licenses/${_realname}

  # Readme
  cd "${pkgdir}${MINGW_PREFIX}/share/doc/${_realname}"
  markdown README.md > README.html
  rm README.md
}
